package com.ruoyi.inventory.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.inventory.service.IWCheckGoodsRelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.WCheckGoodsInfo;
import com.ruoyi.inventory.service.IWCheckGoodsInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盘库作业Controller
 * 
 * @author wzy
 * @date 2024-08-09 10:38:51
 */
@RestController
@RequestMapping("/inventory/task")
public class WCheckGoodsInfoController extends BaseController
{
    @Autowired
    private IWCheckGoodsInfoService wCheckGoodsInfoService;

    @Autowired
    private IWCheckGoodsRelService wCheckGoodsRelService;

    /**
     * 查询盘库作业列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(WCheckGoodsInfo wCheckGoodsInfo)
    {
        startPage();
        List<WCheckGoodsInfo> list = wCheckGoodsInfoService.selectWCheckGoodsInfoList(wCheckGoodsInfo);
        return getDataTable(list);
    }

    /**
     * 导出盘库作业列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:task:export')")
    @Log(title = "盘库作业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WCheckGoodsInfo wCheckGoodsInfo)
    {
        List<WCheckGoodsInfo> list = wCheckGoodsInfoService.selectWCheckGoodsInfoList(wCheckGoodsInfo);
        ExcelUtil<WCheckGoodsInfo> util = new ExcelUtil<WCheckGoodsInfo>(WCheckGoodsInfo.class);
        util.exportExcel(response, list, "盘库作业数据");
    }

    /**
     * 获取盘库作业详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:task:query')")
    @GetMapping(value = "/{checkGoodsId}")
    public AjaxResult getInfo(@PathVariable("checkGoodsId") Long checkGoodsId)
    {
        return success(wCheckGoodsInfoService.selectWCheckGoodsInfoByCheckGoodsId(checkGoodsId));
    }

    /**
     * 新增盘库作业
     */
    @PreAuthorize("@ss.hasPermi('inventory:task:add')")
    @Log(title = "盘库作业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WCheckGoodsInfo wCheckGoodsInfo)
    {
        wCheckGoodsInfo.setCheckGoodsNumber(OrderUtils.getOrderNumber("CG", "-"));
        wCheckGoodsInfo.setCreateBy(getNickName());
        return toAjax(wCheckGoodsInfoService.insertWCheckGoodsInfo(wCheckGoodsInfo));
    }

    /**
     * 修改盘库作业
     */
    @PreAuthorize("@ss.hasPermi('inventory:task:edit')")
    @Log(title = "盘库作业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WCheckGoodsInfo wCheckGoodsInfo)
    {
        wCheckGoodsInfo.setUpdateBy(getNickName());
        return toAjax(wCheckGoodsInfoService.updateWCheckGoodsInfo(wCheckGoodsInfo));
    }

    /**
     * 删除盘库作业
     */
    @PreAuthorize("@ss.hasPermi('inventory:task:remove')")
    @Log(title = "盘库作业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{checkGoodsIds}")
    public AjaxResult remove(@PathVariable Long[] checkGoodsIds)
    {
        wCheckGoodsRelService.deleteWCheckGoodsRelByCheckGoodsIds(checkGoodsIds);
        return toAjax(wCheckGoodsInfoService.deleteWCheckGoodsInfoByCheckGoodsIds(checkGoodsIds));
    }

    /**
     * 修改盘库单状态，确认完成
     */
    @PreAuthorize("@ss.hasPermi('inventory:task:confirm')")
    @Log(title = "确认完成", businessType = BusinessType.CONFIRM)
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody WCheckGoodsInfo wCheckGoodsInfo)
    {
//        logger.debug("WWW " + "wCheckGoodsInfo = " + wCheckGoodsInfo);
        wCheckGoodsInfo.setUpdateBy(getNickName());
        wCheckGoodsInfo.setCheckGoodsConfirmPerson(getNickName());
        wCheckGoodsInfo.setCheckGoodsConfirmTime(DateUtils.getNowDate());
        wCheckGoodsInfo.setJobStatus("3");
        return toAjax(wCheckGoodsInfoService.updateWCheckGoodsInfo(wCheckGoodsInfo));
    }

    /**
     * 修改盘库单状态，撤销完成
     */
    @PreAuthorize("@ss.hasPermi('inventory:receipt:revoke')")
    @Log(title = "撤销完成", businessType = BusinessType.REVOKE)
    @PutMapping("/revoke")
    public AjaxResult revoke(@RequestBody WCheckGoodsInfo wCheckGoodsInfo)
    {
        //logger.debug("WWW " + "wCheckGoodsInfo = " + wCheckGoodsInfo);
        wCheckGoodsInfo.setUpdateBy(getNickName());
        wCheckGoodsInfo.setCheckGoodsConfirmPerson(null);
        wCheckGoodsInfo.setCheckGoodsConfirmTime(null);
        wCheckGoodsInfo.setJobStatus("2");
        return toAjax(wCheckGoodsInfoService.updateWCheckGoodsInfo(wCheckGoodsInfo));
    }

}
